package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.listen.NewOutstandingListener;
import com.yearEndWorkload.mapper.OutstandingChildMapper;
import com.yearEndWorkload.mapper.OutstandingPerformanceMapper;
import com.yearEndWorkload.mapper.TypeStandardMapper;
import com.yearEndWorkload.pojo.excel.exports.NewOutstandingExportExcel;
import com.yearEndWorkload.pojo.excel.importTemplate.NewOutstandingExcel;
import com.yearEndWorkload.pojo.form.add.AddOutstandingPerformanceChildForm;
import com.yearEndWorkload.pojo.form.add.AddOutstandingPerformanceForm;
import com.yearEndWorkload.pojo.form.update.UpdateOutstandingPerformanceChildForm;
import com.yearEndWorkload.pojo.form.update.UpdateOutstandingPerformanceForm;
import com.yearEndWorkload.pojo.query.OutstandingQuery;
import com.yearEndWorkload.service.impl.OutstandingPerformanceServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/s3/outstanding")
@Tag(name = "新增突出业绩成果奖励管理", description = "新增突出业绩成果奖励管理")
public class OutstandingController {

    @Autowired
    OutstandingPerformanceMapper outstandingPerformanceMapper;

    @Autowired
    OutstandingChildMapper outstandingChildMapper;

    @Autowired
    TypeStandardMapper typeStandardMapper;

    @Autowired
    OutstandingPerformanceServiceImpl outstandingPerformanceService;

    @Operation(summary = "总-Excel批量导入新增突出业绩成果奖励管理信息", description = "Excel批量导入新增突出业绩成果奖励管理信息")
    @PostMapping("/addByBatch")
    @Transactional
    public Result<Map<String, Object>> addBatch(@RequestParam("file") @Parameter(description = "添加Excel文件") MultipartFile file) throws IOException {

        NewOutstandingListener newOutstandingListener = new NewOutstandingListener();
        newOutstandingListener.setOutstandingChildMapper(outstandingChildMapper);
        newOutstandingListener.setOutstandingPerformanceMapper(outstandingPerformanceMapper);
        newOutstandingListener.setTypeStandardMapper(typeStandardMapper);
        EasyExcel.read(file.getInputStream(), NewOutstandingExcel.class, newOutstandingListener).sheet().doRead();

        Map<String, Object> map = new HashMap<>();

        List<NewOutstandingExcel> unSave = newOutstandingListener.getUnSave();
        map.put("unSave", unSave);

        return Result.success(map);
    }

    @Operation(summary = "总-列表查询获取", description = "列表查询获取")
    @PostMapping("/getList")
    public Result getList(@RequestBody OutstandingQuery outstandingQuery) {
        return outstandingPerformanceService.getList(outstandingQuery);
    }

    @Operation(summary = "父-添加一个新增突出业绩成果奖励管理", description = "添加一个新增突出业绩成果奖励管理")
    @PostMapping("/addOutstandingPerformance")
    public Result addOutstandingPerformance(@RequestBody AddOutstandingPerformanceForm addOutstandingPerformanceForm) {
        return outstandingPerformanceService.addOutstandingPerformance(addOutstandingPerformanceForm);
    }

    @Operation(summary = "父-更改一个新增突出业绩成果奖励管理", description = "更改一个新增突出业绩成果奖励管理")
    @PostMapping("/updateOutstandingPerformance")
    public Result updateOutstandingPerformance(@RequestBody UpdateOutstandingPerformanceForm updateOutstandingPerformanceForm) {
        return outstandingPerformanceService.updateOutstandingPerformance(updateOutstandingPerformanceForm);
    }

    @Operation(summary = "父-删除多个新增突出业绩成果奖励管理", description = "删除多个新增突出业绩成果奖励管理")
    @DeleteMapping("/deleteOutstandingPerformanceByIds")
    public Result deleteByIds(@RequestBody List<String> list) {
        return outstandingPerformanceService.deleteOutstandingPerformanceByIds(list);
    }

    @Operation(summary = "子-添加一个子业绩", description = "添加一个子业绩")
    @PostMapping("/addOutstandingPerformanceChild")
    public Result addOutstandingPerformanceChild(@RequestBody AddOutstandingPerformanceChildForm addOutstandingPerformanceChildForm) {
        return outstandingPerformanceService.addOutstandingPerformanceChild(addOutstandingPerformanceChildForm);
    }

    @Operation(summary = "子-更改一个子业绩", description = "更改一个子业绩")
    @PostMapping("/updateOutstandingPerformanceChild")
    public Result updateOutstandingPerformanceChild(@RequestBody UpdateOutstandingPerformanceChildForm updateOutstandingPerformanceChildForm) {
        return outstandingPerformanceService.updateOutstandingPerformanceChild(updateOutstandingPerformanceChildForm);
    }

    @Operation(summary = "子-删除多个子业绩", description = "删除多个子业绩")
    @DeleteMapping("/deleteOutstandingPerformanceChildByIds")
    public Result deleteOutstandingPerformanceChildByIds(@RequestBody List<String> list) {
        return outstandingPerformanceService.deleteOutstandingPerformanceChildByIds(list);
    }

    @Operation(summary = "Excel导出新增突出业绩成果奖励管理信息",description = "Excel导出新增突出业绩成果奖励管理信息")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response){
        ExcelResProp.setExcelRespProp(response, "新增突出业绩成果奖励管理");
        List<NewOutstandingExportExcel> outstandingExportExcels = outstandingPerformanceService.getOutstandingExportExcels();
        EasyExcel.write(response.getOutputStream())
                .head(NewOutstandingExportExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("新增突出业绩成果奖励管理")
                .doWrite(outstandingExportExcels);
    }



}
